/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package roles;

import avenue.ErrorBox;
import avenue.MyWindowEvent;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import javax.persistence.EntityManager;
import model.DBManager;
import model.Module;
import model.RolePermission;

/**
 *
 * @author Zerf
 */
public class EditPermissions extends javax.swing.JFrame {

    public EditPermissions() {
        em = DBManager.em;
        initComponents();
    }
    
    //Καλείται όταν ζητάμε επεξεργασία
    public EditPermissions(RolePermission rp) {
        em = DBManager.em;
        this.rp=rp;
        initComponents();
        roleTextField.setText(rp.getRoleId().getRoleName());
        moduleComboBox.setEditable(false);
        String name=rp.getModuleId().getDescription();
        moduleComboBox.setModel(new javax.swing.DefaultComboBoxModel(new String[] { name }));
        createCB.setSelected(rp.getCanCreate()==null?false:true);
        retrieveCB.setSelected(rp.getCanRetrieve()==null?false:true);
        updateCB.setSelected(rp.getCanUpdate()==null?false:true);
        deleteCB.setSelected(rp.getCanDelete()==null?false:true);
        slaCB.setSelected(rp.getStationLevelAccess()==null?false:true);
    }

    //Καλείται όταν ζητάμε δημιουργία νέας εγγραφής
    public EditPermissions(RolePermission rp, String[] cblist) {
        em = DBManager.em;
        this.rp=rp;
        this.cblist=cblist;
        initComponents();
        roleTextField.setText(rp.getRoleId().getRoleName());
        moduleComboBox.setEditable(true);
        createCB.setSelected(rp.getCanCreate()==null?false:true);
        retrieveCB.setSelected(rp.getCanRetrieve()==null?false:true);
        updateCB.setSelected(rp.getCanUpdate()==null?false:true);
        deleteCB.setSelected(rp.getCanDelete()==null?false:true);
        slaCB.setSelected(rp.getStationLevelAccess()==null?false:true);
    }
    
    
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {
        bindingGroup = new org.jdesktop.beansbinding.BindingGroup();

        rolePermission1 = new model.RolePermission();
        moduleQuery = java.beans.Beans.isDesignTime() ? null : em.createQuery("select m from Module m");
        moduleList = java.beans.Beans.isDesignTime() ? java.util.Collections.emptyList() : moduleQuery.getResultList();
        moduleBoxRenderer1 = new avenue.ModuleBoxRenderer();
        jLabel1 = new javax.swing.JLabel();
        roleTextField = new javax.swing.JTextField();
        moduleComboBox = new javax.swing.JComboBox();
        jLabel2 = new javax.swing.JLabel();
        createCB = new javax.swing.JCheckBox();
        retrieveCB = new javax.swing.JCheckBox();
        updateCB = new javax.swing.JCheckBox();
        deleteCB = new javax.swing.JCheckBox();
        slaCB = new javax.swing.JCheckBox();
        saveButton = new javax.swing.JButton();
        cancelButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        jLabel1.setText("Ρόλος:");

        roleTextField.setEditable(false);

        org.jdesktop.beansbinding.Binding binding = org.jdesktop.beansbinding.Bindings.createAutoBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, rolePermission1, org.jdesktop.beansbinding.ELProperty.create("${roleId.roleName}"), roleTextField, org.jdesktop.beansbinding.BeanProperty.create("text"));
        bindingGroup.addBinding(binding);

        moduleComboBox.setRenderer(moduleBoxRenderer1);

        org.jdesktop.swingbinding.JComboBoxBinding jComboBoxBinding = org.jdesktop.swingbinding.SwingBindings.createJComboBoxBinding(org.jdesktop.beansbinding.AutoBinding.UpdateStrategy.READ_WRITE, moduleList, moduleComboBox);
        bindingGroup.addBinding(jComboBoxBinding);

        moduleComboBox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                moduleComboBoxActionPerformed(evt);
            }
        });

        jLabel2.setText("Υποσύστημα:");

        createCB.setText("Εισαγωγή");
        createCB.setToolTipText("");
        createCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                createCBActionPerformed(evt);
            }
        });

        retrieveCB.setText("Αναζήτηση");
        retrieveCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                retrieveCBActionPerformed(evt);
            }
        });

        updateCB.setText("Επεξεργασία");
        updateCB.setToolTipText("");
        updateCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                updateCBActionPerformed(evt);
            }
        });

        deleteCB.setText("Διαγραφή");
        deleteCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                deleteCBActionPerformed(evt);
            }
        });

        slaCB.setText("Ορατότητα Σταθμού");
        slaCB.setToolTipText("");
        slaCB.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                slaCBActionPerformed(evt);
            }
        });

        saveButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/save.gif"))); // NOI18N
        saveButton.setText("Αποθήκευση");
        saveButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveButtonActionPerformed(evt);
            }
        });

        cancelButton.setIcon(new javax.swing.ImageIcon(getClass().getResource("/images/cancel.gif"))); // NOI18N
        cancelButton.setText("Άκυρο");
        cancelButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel1)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(roleTextField))
                    .addComponent(createCB)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(moduleComboBox, 0, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                    .addComponent(slaCB)
                    .addComponent(deleteCB)
                    .addComponent(updateCB)
                    .addComponent(retrieveCB)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(saveButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(cancelButton)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(roleTextField, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(moduleComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(createCB)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(retrieveCB)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(updateCB)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(deleteCB)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(slaCB)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(saveButton)
                    .addComponent(cancelButton))
                .addContainerGap(14, Short.MAX_VALUE))
        );

        bindingGroup.bind();

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void createCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createCBActionPerformed
        rp.setCanCreate(createCB.isSelected()?new Integer(1):null);
    }//GEN-LAST:event_createCBActionPerformed

    private void saveButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveButtonActionPerformed
        String selected=moduleComboBox.getSelectedItem().toString();
        boolean valid=false;
        //Ελέγχεται εάν έχει ήδη χρησιμοποιηθεί αυτό το δικαίωμα για τον συγκεκριμένο ρόλο.
        if (this.getTitle().equalsIgnoreCase("Εισαγωγή Δικαιώματος")){
            if (cblist!=null) for(int i=0;i<cblist.length;i++)
                if (selected.equalsIgnoreCase(cblist[i])) valid=true;
            if (!valid){
                    (new ErrorBox("Έχουν ήδη οριστεί δικαιώματα για το συγκεκριμένο υποσύστημα!")).setVisible(true);
                    return;
            }
        }
        MyWindowEvent we = new MyWindowEvent(this, WindowEvent.WINDOW_CLOSED,true);
        for (WindowListener l : this.getWindowListeners()) {
            l.windowClosed(we);
        }
        this.setVisible(false); 
    }//GEN-LAST:event_saveButtonActionPerformed

    private void retrieveCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_retrieveCBActionPerformed
        rp.setCanRetrieve(retrieveCB.isSelected()?new Integer(1):null);
    }//GEN-LAST:event_retrieveCBActionPerformed

    private void updateCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_updateCBActionPerformed
        rp.setCanUpdate(updateCB.isSelected()?new Integer(1):null);
    }//GEN-LAST:event_updateCBActionPerformed

    private void deleteCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_deleteCBActionPerformed
        rp.setCanDelete(deleteCB.isSelected()?new Integer(1):null);
    }//GEN-LAST:event_deleteCBActionPerformed

    private void slaCBActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_slaCBActionPerformed
        rp.setStationLevelAccess(slaCB.isSelected()?new Integer(1):null);
    }//GEN-LAST:event_slaCBActionPerformed

    private void cancelButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelButtonActionPerformed
        // TODO add your handling code here:
        MyWindowEvent we = new MyWindowEvent(this, WindowEvent.WINDOW_CLOSED,false);
        for (WindowListener l : this.getWindowListeners()) {
            l.windowClosed(we);
        }
        this.setVisible(false); 
    }//GEN-LAST:event_cancelButtonActionPerformed

    private void moduleComboBoxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_moduleComboBoxActionPerformed
        // TODO add your handling code here:
        rp.setModuleId((Module)moduleComboBox.getSelectedItem());
    }//GEN-LAST:event_moduleComboBoxActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(EditPermissions.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(EditPermissions.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(EditPermissions.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(EditPermissions.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new EditPermissions().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton cancelButton;
    private javax.swing.JCheckBox createCB;
    private javax.swing.JCheckBox deleteCB;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private avenue.ModuleBoxRenderer moduleBoxRenderer1;
    private javax.swing.JComboBox moduleComboBox;
    private java.util.List moduleList;
    private javax.persistence.Query moduleQuery;
    private javax.swing.JCheckBox retrieveCB;
    private model.RolePermission rolePermission1;
    private javax.swing.JTextField roleTextField;
    private javax.swing.JButton saveButton;
    private javax.swing.JCheckBox slaCB;
    private javax.swing.JCheckBox updateCB;
    private org.jdesktop.beansbinding.BindingGroup bindingGroup;
    // End of variables declaration//GEN-END:variables
    RolePermission rp;
    EntityManager em;
    String[] cblist;
}
